home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1032 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  6.6 KB

  1. From: gregor@netcom.com (Greg Colvin)
  2. Message-ID: <gregorDpoGM2.Hw2@netcom.com>
  3. X-Original-Date: Thu, 11 Apr 1996 03:06:02 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 11 Apr 96 08:05:39 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: sample auto_ptr template
  9. Organization: Netcom Online Communications Services (408-241-9760 login: guest)
  10. References: <009A0A5CE1159CC0.49802F14@ittpub.nl>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMWy90+EDnX0m9pzZAQEbagGAmUmhXMA9JJF1Q0F0Kumm8Hze34KKX6VW
  13.     QrBDR5VwCUJbZ5Rg3Uj29BdLPAKoC2hk
  14.     =3wCG
  15.  
  16. In article <009A0A5CE1159CC0.49802F14@ittpub.nl> "Wil Evers" <wil@ittpub.nl>
  17. writes:
  18. >In article <gregorDpFBCt.A5@netcom.com> gregor@netcom.com (Greg
  19. >Colvin) writes:
  20. >
  21. >> Please remember that auto_ptr is designed to make sure that pointers
  22. >> are deleted when exceptions are thrown, not to be sure that already
  23. >> deleted pointers are not misused.  The semantics of auto_ptr are
  24. >> intentionally "as close as possible" to ordinary pointers: if you
  25. >> use a pointer that is already deleted the behaviour is undefined,
  26. >> and the same for auto_ptr; if you use a pointer that is owned by
  27. >> another object (i.e. will be deleted by that objects destructor) the
  28. >> behaviour is well defined as long as the pointer remains valid, and
  29. >> the same for auto_ptr; you cannot tell at runtime whether a pointer
  30. >> is valid, and the same for auto_ptr.
  31. >
  32. >My original question was "Did the committee explicitly decide to allow
  33. >dereferencing of non-owning auto_ptrs?  If so, what is the rationale
  34. >behind this?"  It seems to me Greg's answer is "Yes, because that's
  35. >what ordinary pointers do."  I cannot understand this.  Why allow a
  36. >dangerous operation just because ordinary pointers do not prohibit it?
  37. >
  38. >Please note:
  39. >
  40. >1. The previous (April '95 DWP) incarnation of auto_ptr did not forbid
  41. >to have a second, non-owning, pointer to the owned object.  However,
  42. >in order to get one, we had to explicitly call the get() member
  43. >function, which returns an ordinary pointer.  In my opinion, requiring
  44. >the user to go through all this trouble was a reasonable safeguard
  45. >against accessing released memory and unintended aliasing.  In the new
  46. >incarnation, there are no such guarantees: an auto_ptr silently
  47. >changes into a zombie pointer as a side effect of a copy operation.
  48. >
  49.  
  50. Pre April, an auto_ptr silently changed into a null pointer after
  51. copying, except that trying to copy it was ill-formed.  Post April
  52. copying is allowed.  Derereferencing a null pointer gives undefined
  53. behavior, as does dereferencing a pointer to a deleted object, so I
  54. don't see such a big difference. Given
  55.    void f(auto_ptr<T>);
  56.    auto_ptr<T> p(px);
  57. then before April we had
  58.    f(p);            // unintentionally ill-formed
  59.    p->whatever();   // undefined behavior
  60. and after April we have
  61.    f(p);            // well-formed
  62.    p->whatever();   // possibly undefined behavior
  63.  
  64. It is not hard to create a version of auto_ptr that throws an exception
  65. if the "zombie pointer" is dereferenced, at some cost in performance,
  66. and I hope vendors will provide such versions for debugging at least.
  67. If an implementation provides a garbage collector (which is hard) then
  68. providing a safer auto_ptr becomes fairly easy and cheap.  Vendors are
  69. always allowed to define undefined behavior to something more useful.
  70.  
  71. >2.  We all have to pay the price for the committee's decision.  The
  72. >new implementation is about twice as complicated - up to the point
  73. >where almost no existing compiler can handle it - and half as
  74. >efficient.  Furthermore, dropping a guarantee always breaks existing
  75. >code.
  76.  
  77. Actually, no compiler has ever been able to compile any version of
  78. auto_ptr, since they all have used member templates.
  79.  
  80. My implementation of auto_ptr is not, IMHO, significantly more
  81. complicated that before (one more member and a few tests, no big
  82. deal).  The complication has always been in the member templates.
  83.  
  84. >
  85. >What I definitely do not understand is why the new auto_ptr template
  86. >doesn't even allow us to query if it is actually owning the object
  87. >pointed to. 
  88. >
  89.  
  90. This was a hard call, and I'm open to suggestions.  Adding a mutable
  91. member to a class is never pretty, but making the member visible in any
  92. way is ugly:  I don't like for a const argument to change its value,
  93. and I think of the value of an object as being whatever its public
  94. interface reveals.  Thus, I find "mutable bool owner" just barely
  95. tolerable, and "bool is_owner() { return owner;} " quite distasteful.
  96.  
  97. As a practical matter, I can imagine implementations of auto_ptr that
  98. do not store the ownership in the auto_ptr, but perhaps in the bowels
  99. of an OS provided garbage collection system, such that is_owner() might
  100. be very hard to implement efficiently.
  101.  
  102. Also, I found it very hard to contrive examples where is_owner() would
  103. actually be useful.  I think most such examples would involve passing a
  104. reference to an auto_ptr to a function that may or may not then take
  105. ownership.  In that case, the callee can always zero out the auto_ptr
  106. argument itself.  An alternative idiom is:
  107.    template<typename T> auto_ptr<T> source_sink(auto_ptr<T>);
  108.    ...
  109.    p = source_sink(p);
  110.    if (&*p)
  111.       ...
  112.  
  113. >> What you can do is design your code so that you know who owns each
  114. >> object when.  The auto_ptr template can help you implement such
  115. >> designs safely.  If you cannot design your code to work with
  116. >> auto_ptr you probably need garbage collection of some form.
  117. >
  118. >auto_ptr may have been designed to provide only limited functionality,
  119. >but since it is the only heap object management facility in the
  120. >standard, it will be used in many different contexts.  Therefore, I
  121. >think it is quite resonable to ask how easy it is to abuse it.  What I
  122. >dislike about the new auto_ptr incarnation is that it is even easier
  123. >to abuse
  124.  
  125. I still think that being able to tell who owns an auto_ptr will lead even 
  126. more "abuse", since if need to ask you will inevitably forget to ask, and
  127. crash as a result.  And if you need to know, then your design is probably
  128. not appropriate for an auto_ptr<> anyway.  Don't ask, don't tell.
  129.  
  130. If anyone can think of examples where an is_owner() is really required
  131. please let me know.  A workable fudge might be a protected is_owner(),
  132. so that you have to work at abusing it.
  133.  
  134. Greg Colvin
  135. gregor@netcom.com
  136. ---
  137. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  138. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  139. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  140. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  141. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  142.